﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Super.Common
{
    public class TimeParser
    {

        /// <summary>
        /// 将Unix时间戳转换为C#系统时间DateTime格式
        /// </summary>
        /// <param name="d">10位int</param>
        /// <returns>DateTime</returns>
        public static System.DateTime ConvertIntToDateTime(int d)
        {
            System.DateTime time = System.DateTime.MinValue;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            time = startTime.AddSeconds((double)d);

            return time;
        }
        /// <summary>
        /// 将String类型的Unix时间戳转换为C#系统时间DateTime格式
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static System.DateTime ConvertIntToDateTime(string d)
        {
            int dd;
            try
            {
                dd = int.Parse(d);
                return ConvertIntToDateTime(dd);
            }
            catch
            {
                return Convert.ToDateTime(d);
            }

            
        }

        /// <summary>
        /// 将c# DateTime时间格式转换为Unix时间戳格式 输出INT 10位
        /// </summary>
        /// <param name="time">DATATIME时间</param>
        /// <returns>int</returns>
        public static string ConvertDateTimeToInt(System.DateTime time)
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (time - startTime).TotalSeconds;
            return intResult.ToString()+time.Millisecond;
        }

        /// <summary>
        /// 把秒转换成分钟
        /// </summary>
        /// <returns></returns>
        public static int SecondToMinute(int Second)
        {
            decimal mm = (decimal)((decimal)Second / (decimal)60);
            return Convert.ToInt32(Math.Ceiling(mm));           
        }

        #region 返回某年某月最后一天
        /// <summary>
        /// 返回某年某月最后一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns>日</returns>
        public static int GetMonthLastDate(int year, int month)
        {
            DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
            int Day = lastDay.Day;
            return Day;
        }
        #endregion
        /// <summary>
        /// 获取当前系统时间 长时间格式
        /// </summary>
        /// <returns></returns>
        public static DateTime GetNowTime()
        {
            return GetNowTime("yyyy-MM-dd HH:mm:ss");
        }
        public static DateTime GetNowTime(string format)
        {
            return Convert.ToDateTime(DateTime.Now.ToString(format));
           
        }

        #region 返回时间差
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;
            try
            {
                //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                //TimeSpan ts = ts1.Subtract(ts2).Duration();
                TimeSpan ts = DateTime2 - DateTime1;
                if (ts.Days >=1)
                {
                    dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
                }
                else
                {
                    if (ts.Hours > 1)
                    {
                        dateDiff = ts.Hours.ToString() + "小时前";
                    }
                    else
                    {
                        dateDiff = ts.Minutes.ToString() + "分钟前";
                    }
                }
            }
            catch
            { }
            return dateDiff;
        }
        /// <summary>
        /// 返回字符时间距离当前时间的剩余天数小时
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string DateDiff(string datetime)
        {
            string dateDiff = null;
            try
            {
                //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                //TimeSpan ts = ts1.Subtract(ts2).Duration();
                DateTime DateTime2 = Convert.ToDateTime(datetime);
                DateTime DateTime1 = GetNowTime();

                TimeSpan ts = DateTime2 - DateTime1;
                if (ts.Days >= 1)
                {
                    dateDiff = ts.Days + "天" + ts.Hours + "小时" + ts.Minutes + "分";
                }
                else
                {
                    dateDiff = ts.Hours + "小时" + ts.Minutes + "分"+ts.Milliseconds+"秒";
                }
            }
            catch
            { }
            return dateDiff;
        }

        /// <summary>
        /// 返回字符时间距离当前时间的剩余天数小时
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string DateDiffByDay(string datetime)
        {
            string dateDiff = null;
            try
            {
                DateTime DateTime2 = Convert.ToDateTime(datetime);
                DateTime DateTime1 = GetNowTime();

                TimeSpan ts = DateTime2 - DateTime1;
                if (ts.Days >= 1)
                {
                    dateDiff = ts.Days + "天";
                }
                else if(ts.Hours>=1)
                {
                    dateDiff = ts.Hours + "小时";
                }
                else if (ts.Minutes >= 1)
                {
                    dateDiff = ts.Minutes + "分钟";
                }
                else if (ts.Seconds >= 1)
                {
                    dateDiff = ts.Seconds + "秒";
                }
                else
                {
                    dateDiff = "";
                }
            }
            catch
            { }
            return dateDiff;
        }

        
        #endregion
    }
}
